clear
baseline=1;%ebp shock
jq=0;%JERMANN QUADRINI SHOCK
bcdz=0;%BANKING SUPPLY SHOCK
cmr=0;%CMR SHOCK
text=0;%MUMATZ TEXTUAL SHOCK
proxy=0;%PROXY VAR
just_figure=0;%only do figure
cpi_levels=0;%PUT CPI IN LOG-LEVELS
post_1982=0;%POST-1982 SAMPLE
if post_1982==0
    b=1;
else
 b=41;
end
if just_figure==0
  if baseline==1||proxy==1
load data_baseline_with_rpi;
  data(:,1)=[data(1,1);diff(data(:,1))];
  end
  if cpi_levels==1
load data_baseline_with_rpi;
  data(:,1)=[data(1,1);diff(data(:,1))];
  data(:,8)=cumsum(data(:,8));
  end
  if jq==1
load data_baseline_with_rpi_jq;
  data(:,1)=-zscore([data(1,1);diff(data(:,1))]);
  end
  if bcdz==1
load data_baseline_with_rpi_bcdz;
  data(:,1)=zscore([data(1,1);diff(data(:,1))]);
  end
  if cmr==1
load data_baseline_with_rpi_cmr;
  data(:,1)=zscore([data(1,1);diff(data(:,1))]);
  end
  if text==1
load data_baseline_with_rpi_textual;
  data(:,1)=zscore([data(1,1);diff(data(:,1))]);
  end
data(:,3)=[];
if proxy==1
    data(:,end)=[];
end
% data=data(:,[3,4,5,7,1,2,6,8,9]);
parfor i=1:2000
    if proxy==0
[~,~,usa]=bootstrap_ISTC_news(data(b:end,:),1,5,40,41,1,3,1,1,1,1,size(data,2)-1,0);
    else
 [~,~,usa]=bootstrap_ISTC_news(data(b:end,:),1,5,40,41,1,3,1,1,1,1,1,0);
    end
rt(:,:,i)=usa.TFP;
vt(:,:,i)=usa.weight_tfp;
cs_shock(:,i)=usa.shocks(:,1);
end
median_shock=median(cs_shock,2);
ir=median(rt,3);ir_qu=quantile(rt,[0.025,0.975],3);ir_min=nan(60,5);ir_max=nan(60,5);
vv=median(vt,3);vv_min=nan(60,5);vv_max=nan(60,5);vv_qu=quantile(vt,[0.025,0.975],3);
else
    if post_1982==0
   load RESULTS_BASELINE_97.5_2.5_PERCENTILES.mat
    else
      load RESULTS_1982_97.5_2.5_PERCENTILES.mat 
    end
end
h=20;
x=1:h;
figure
if baseline==1||proxy==1||cpi_levels==1
    subplot(3,3,1),plot(x,100*ir(1:h,1),'b-',x,100*ir_qu(1:h,1,2),'b--',x,100*ir_min(1:h,1),'b:',x,100*ir_max(1:h,1),'b:',x,100*ir_qu(1:h,1,1),'b--','LineWidth',3);
title('EBP'); xlabel('Horizon');ylabel('Basis Point Deviation');
end
if jq==1
    subplot(3,3,1),plot(x,1*ir(1:h,1),'b-',x,1*ir_qu(1:h,1,2),'b--',x,1*ir_min(1:h,1),'b:',x,1*ir_max(1:h,1),'b:',x,1*ir_qu(1:h,1,1),'b--','LineWidth',3);
title('JQ'); xlabel('Horizon');ylabel('Standard Deviation Units');
end
if cmr==1
        subplot(3,3,1),plot(x,1*ir(1:h,1),'b-',x,1*ir_qu(1:h,1,2),'b--',x,1*ir_min(1:h,1),'b:',x,1*ir_max(1:h,1),'b:',x,1*ir_qu(1:h,1,1),'b--','LineWidth',3);
title('CMR'); xlabel('Horizon');ylabel('Standard Deviation Units');
end
if bcdz==1
        subplot(3,3,1),plot(x,1*ir(1:h,1),'b-',x,1*ir_qu(1:h,1,2),'b--',x,1*ir_min(1:h,1),'b:',x,1*ir_max(1:h,1),'b:',x,1*ir_qu(1:h,1,1),'b--','LineWidth',3);
title('BCDZ'); xlabel('Horizon');ylabel('Standard Deviation Units');
end
if text==1
        subplot(3,3,1),plot(x,1*ir(1:h,1),'b-',x,1*ir_qu(1:h,1,2),'b--',x,1*ir_min(1:h,1),'b:',x,1*ir_max(1:h,1),'b:',x,1*ir_qu(1:h,1,1),'b--','LineWidth',3);
title('MPT'); xlabel('Horizon');ylabel('Standard Deviation Units');
end
subplot(3,3,2),plot(x,100*ir(1:h,2),'b-',x,100*ir_qu(1:h,2,2),'b--',x,100*ir_min(1:h,2),'b:',x,100*ir_max(1:h,2),'b:',x,100*ir_qu(1:h,2,1),'b--','LineWidth',3);
title('TFP'); xlabel('Horizon');ylabel('Percentage Deviation');
 subplot(3,3,3),plot(x,100*ir(1:h,3),'b-',x,100*ir_qu(1:h,3,2),'b--',x,100*ir_min(1:h,3),'b:',x,100*ir_max(1:h,3),'b:',x,100*ir_qu(1:h,3,1),'b--','LineWidth',3);
title('Output');  xlabel('Horizon');ylabel('Percentage Deviation');
subplot(3,3,4),plot(x,100*ir(1:h,4),'b-',x,100*ir_qu(1:h,4,2),'b--',x,100*ir_min(1:h,4),'b:',x,100*ir_max(1:h,4),'b:',x,100*ir_qu(1:h,4,1),'b--','LineWidth',3);
title('Investment');  xlabel('Horizon');ylabel('Percentage Deviation');
subplot(3,3,5),plot(x,100*ir(1:h,5),'b-',x,100*ir_qu(1:h,5,2),'b--',x,100*ir_min(1:h,5),'b:',x,100*ir_max(1:h,5),'b:',x,100*ir_qu(1:h,5,1),'b--','LineWidth',3);
title('Consumption'); xlabel('Horizon');ylabel('Percentage Deviation');
 subplot(3,3,6),plot(x,100*ir(1:h,6),'b-',x,100*ir_qu(1:h,6,2),'b--',x,100*ir_min(1:h,5),'b:',x,100*ir_max(1:h,5),'b:',x,100*ir_qu(1:h,6,1),'b--','LineWidth',3);
title('Hours');  xlabel('Horizon');ylabel('Percentage Deviation');
 subplot(3,3,7),plot(x,100*ir(1:h,8),'b-',x,100*ir_qu(1:h,8,2),'b--',x,100*ir_min(1:h,5),'b:',x,100*ir_max(1:h,5),'b:',x,100*ir_qu(1:h,8,1),'b--','LineWidth',3);
title('Interest Rate');  xlabel('Horizon');ylabel('Basis Point Deviation');
 subplot(3,3,8),plot(x,100*ir(1:h,7),'b-',x,100*ir_qu(1:h,7,2),'b--',x,100*ir_min(1:h,5),'b:',x,100*ir_max(1:h,5),'b:',x,100*ir_qu(1:h,7,1),'b--','LineWidth',3);
if cpi_levels==0
 title('Inflation');  xlabel('Horizon');ylabel('Percentage Point Deviation');%legend('Median','84th and 16th Percentiles','Location', [0.70, 0.25, 0.05, 0.05]);%[Position_Right_in_pct, Position_Top_in_pct, Horizontal_Stretch, Vertical_Stretch]
else
  title('Prices');  xlabel('Horizon');ylabel('Percentage Deviation');%legend('Median','84th and 16th Percentiles','Location', [0.70, 0.25, 0.05, 0.05]);%[Position_Right_in_pct, Position_Top_in_pct, Horizontal_Stretch, Vertical_Stretch]
 end
 set(gcf,'position',get(0,'screensize'))
if post_1982==0
    if baseline==1
 export_fig(sprintf('BZ_TFP_BASELINE_IRF.pdf'),'-transparent');
% export_fig(sprintf('BZ_TFP_SOLOW_IRF.pdf'),'-transparent');
    end
    if proxy==1
 export_fig(sprintf('BZ_TFP_BASELINE_IRF_PROXY.pdf'),'-transparent');
% export_fig(sprintf('BZ_TFP_SOLOW_IRF.pdf'),'-transparent');
    end
    if cpi_levels==1
 export_fig(sprintf('BZ_TFP_BASELINE_IRF_CPI.pdf'),'-transparent');
    end 
    if jq==1
 export_fig(sprintf('BZ_TFP_BASELINE_IRF_JQ.pdf'),'-transparent');
    end 
    if bcdz==1
 export_fig(sprintf('BZ_TFP_BASELINE_IRF_BCDZ.pdf'),'-transparent');
    end
    if cmr==1
 export_fig(sprintf('BZ_TFP_BASELINE_IRF_CMR.pdf'),'-transparent');
    end 
    if text==1
 export_fig(sprintf('BZ_TFP_BASELINE_IRF_TEXT.pdf'),'-transparent');
    end  
else
export_fig(sprintf('BZ_TFP_1982_IRF.pdf'),'-transparent');
end
    
close all
figure
subplot(3,3,1),plot(x,vv(1:h,1),'b-',x,vv_qu(1:h,1,2),'b--',x,100*ir_min(1:h,1),'b:',x,100*ir_max(1:h,1),'b:',x,vv_qu(1:h,1,1),'b--','LineWidth',3);
if baseline==1||proxy==1
title('EBP'); xlabel('Horizon');ylabel('Proportion of Forecast Error');
end
if jq==1
title('JQ'); xlabel('Horizon');ylabel('Proportion of Forecast Error');
end
if cmr==1
title('CMR'); xlabel('Horizon');ylabel('Proportion of Forecast Error');
end
if bcdz==1
title('BCDZ'); xlabel('Horizon');ylabel('Proportion of Forecast Error');
end
if text==1
title('MPT'); xlabel('Horizon');ylabel('Proportion of Forecast Error');
end
subplot(3,3,2),plot(x,vv(1:h,2),'b-',x,vv_qu(1:h,2,2),'b--',x,100*ir_min(1:h,2),'b:',x,100*ir_max(1:h,2),'b:',x,vv_qu(1:h,2,1),'b--','LineWidth',3);
title('TFP'); xlabel('Horizon');ylabel('Proportion of Forecast Error');
 subplot(3,3,3),plot(x,vv(1:h,3),'b-',x,vv_qu(1:h,3,2),'b--',x,100*ir_min(1:h,3),'b:',x,100*ir_max(1:h,3),'b:',x,vv_qu(1:h,3,1),'b--','LineWidth',3);
title('Output');  xlabel('Horizon');ylabel('Proportion of Forecast Error');
subplot(3,3,4),plot(x,vv(1:h,4),'b-',x,vv_qu(1:h,4,2),'b--',x,100*ir_min(1:h,4),'b:',x,100*ir_max(1:h,4),'b:',x,vv_qu(1:h,4,1),'b--','LineWidth',3);
title('Investment');  xlabel('Horizon');ylabel('Proportion of Forecast Error');
subplot(3,3,5),plot(x,vv(1:h,5),'b-',x,vv_qu(1:h,5,2),'b--',x,100*ir_min(1:h,5),'b:',x,100*ir_max(1:h,5),'b:',x,vv_qu(1:h,5,1),'b--','LineWidth',3);
title('Consumption'); xlabel('Horizon');ylabel('Proportion of Forecast Error');
 subplot(3,3,6),plot(x,vv(1:h,6),'b-',x,vv_qu(1:h,6,2),'b--',x,100*ir_min(1:h,5),'b:',x,100*ir_max(1:h,5),'b:',x,vv_qu(1:h,6,1),'b--','LineWidth',3);
title('Hours');  xlabel('Horizon');ylabel('Proportion of Forecast Error');
 subplot(3,3,7),plot(x,vv(1:h,8),'b-',x,vv_qu(1:h,8,2),'b--',x,100*ir_min(1:h,5),'b:',x,100*ir_max(1:h,5),'b:',x,vv_qu(1:h,8,1),'b--','LineWidth',3);
title('Interest Rate');  xlabel('Horizon');ylabel('Proportion of Forecast Error');
 subplot(3,3,8),plot(x,vv(1:h,7),'b-',x,vv_qu(1:h,7,2),'b--',x,100*ir_min(1:h,5),'b:',x,vv_max(1:h,5),'b:',x,vv_qu(1:h,7,1),'b--','LineWidth',3);
if cpi_levels==0
 title('Inflation');  xlabel('Horizon');ylabel('Proportion of Forecast Error');%legend('Median','84th and 16th Percentiles','Location',[0.70, 0.25, 0.05, 0.05]);
else
  title('Prices');  xlabel('Horizon');ylabel('Proportion of Forecast Error');%legend('Median','84th and 16th Percentiles','Location',[0.70, 0.25, 0.05, 0.05]);
end
 set(gcf,'position',get(0,'screensize'))
if post_1982==0
   if baseline==1
 export_fig(sprintf('BZ_TFP_BASELINE_FEV.pdf'),'-transparent');
% export_fig(sprintf('BZ_TFP_SOLOW_FEV.pdf'),'-transparent');
   end
    if proxy==1
 export_fig(sprintf('BZ_TFP_BASELINE_FEV_PROXY.pdf'),'-transparent');
    end 
    if cpi_levels==1
 export_fig(sprintf('BZ_TFP_BASELINE_FEV_CPI.pdf'),'-transparent');
    end 
    if jq==1
 export_fig(sprintf('BZ_TFP_BASELINE_FEV_JQ.pdf'),'-transparent');
    end 
    if bcdz==1
 export_fig(sprintf('BZ_TFP_BASELINE_FEV_BCDZ.pdf'),'-transparent');
    end
    if cmr==1
 export_fig(sprintf('BZ_TFP_BASELINE_FEV_CMR.pdf'),'-transparent');
    end 
    if text==1
 export_fig(sprintf('BZ_TFP_BASELINE_FEV_TEXT.pdf'),'-transparent');
    end  
else
export_fig(sprintf('BZ_TFP_1982_FEV.pdf'),'-transparent');
end    

close all